//
//  MaintenceServiceTypeItemCell.swift
//  koolcar
//
//  Created by xiantian on 2023/11/20.
//

import UIKit

class MaintenceServiceTypeItemCell: UICollectionViewCell {
    var imgView: UIImageView!
    var titleLab: UILabel!
    var bkgView: UIView!
    var selectImg: UIImageView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        bkgView = UIView()
        bkgView.backgroundColor = .white
        bkgView.layer.cornerRadius = 4
        bkgView.layer.masksToBounds = true
        contentView.addSubview(bkgView)
        
        imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.backgroundColor = Theme.eColor
        imgView.clipsToBounds = true
        bkgView.addSubview(imgView)
        
        selectImg = UIImageView()
        selectImg.contentMode = .scaleAspectFit
        selectImg.image = UIImage.init(named: "select_service")
        bkgView.addSubview(selectImg)
        
        titleLab = UILabel()
        titleLab.textColor = Theme.titleLabColor
        titleLab.backgroundColor = UIColor.clear
        titleLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
        titleLab.numberOfLines = 3
        titleLab.lineBreakMode = .byWordWrapping
        titleLab.textAlignment = .center
        titleLab.adjustsFontSizeToFitWidth = true
        bkgView.addSubview(titleLab)
        
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        bkgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        imgView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalTo(0)
            make.height.equalTo(60)
        }
        
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
            make.top.equalTo(imgView.snp.bottom)
        }
        
        selectImg.snp.makeConstraints { make in
            make.left.equalTo(4)
            make.top.equalTo(4)
            make.width.equalTo(14)
            make.height.equalTo(14)
        }
    }
    
    func setMaintenceServiceTypeItemCell(data: ServerProductContentModel) {
        self.titleLab.text = data.name ?? ""
        if let url = URL.init(string: data.appUrlIcon ?? "") {
            self.imgView.sd_setImage(with: url, placeholderImage: UIImage.init(named: "icon_default_place_v"))
        }else{
            self.imgView.image = UIImage.init(named: "icon_default_place_v")
        }
        if data.selectFlag {
            self.selectImg.isHidden = false
        }else{
            self.selectImg.isHidden = true
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
